package com.putao.album.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.util.Loger;
import com.putao.album.util.StringHelper;
import com.putao.album.util.ViewHelper;

/**
 * Created by chen on 15-6-10.
 */
public class WriteStoryDialog extends DialogBuilder {

    private DialogButtonOnClickListener mDialogButtonOnClickListener;
    private EditText mEditText;
    private int mInputlengthLimit = 8;
    private String inputText;
    private TextView tv_count_limit;

    public WriteStoryDialog(Context context) {
        super(context);
    }

    public WriteStoryDialog setDialogButtonOnClickListener(DialogButtonOnClickListener listener) {
        mDialogButtonOnClickListener = listener;
        return this;
    }

    public interface DialogButtonOnClickListener {
        void positiveOnClick(Dialog dialog, String importText);

        void negativeOnClick(Dialog dialog, String importText);
    }

    /**
     * 设置输入内容字符长度限制
     *
     * @param length
     */
    public WriteStoryDialog setInputLimit(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("length is must >=0");
        }
        mInputlengthLimit = length;
        return this;
    }

    public WriteStoryDialog setInputText(String inputText) {
        this.inputText = inputText;
        return this;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.dialog_write_story_layout;
    }

    @Override
    protected void doInitSubView(View view) {
        mEditText = ViewHelper.queryViewById(view, R.id.editText);
        tv_count_limit = ViewHelper.queryViewById(view, R.id.tv_count_limit);
        if (mEditText != null) {
            mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputlengthLimit)});
        }
        if (!StringHelper.isEmpty(inputText)) {
            mEditText.setText(inputText);
            tv_count_limit.setText(inputText.toString().length() + "/100");
        }

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_count_limit.setText(s.toString().length() + "/100");
            }
        });
        TextView tv_message = ViewHelper.queryViewById(view, R.id.tv_message);
        if (!StringHelper.isEmpty(getMessage())) {
            tv_message.setText(getMessage());
        }
        Button positiveButton = ViewHelper.queryViewById(view, R.id.positiveButton);
        Button negativeButton = ViewHelper.queryViewById(view, R.id.negativeButton);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDialogButtonOnClickListener != null && mEditText.getText() != null) {
                    mDialogButtonOnClickListener.positiveOnClick(getDialog(), mEditText.getText().toString());
                }
            }
        });

        negativeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mDialogButtonOnClickListener != null && mEditText.getText() != null) {
                    mDialogButtonOnClickListener.negativeOnClick(getDialog(), mEditText.getText().toString());
                }
            }
        });
    }

    @Override
    protected void doSetupData() {

    }
}